package org.baygo.view.api.impl;

import org.baygo.view.api.PanelInterface;
import org.baygo.view.api.listener.ConnectButtonActionListener;
import org.baygo.view.api.listener.CreateNewConnectButtonActionListener;
import org.baygo.view.api.listener.StartButtonActionListener;
import org.baygo.view.api.listener.StopButtonActionListener;
import org.baygo.view.api.listener.mycomponent.MyButton;
import org.baygo.view.api.util.AdressTableModel;

import javax.swing.*;
import javax.swing.table.TableModel;
import java.awt.*;

public class MyJPanelCreator implements PanelInterface{

	private JTextArea logArea = new JTextArea();
	private TableModel model = new AdressTableModel(1,1);
	private MyButton newButton = new MyButton("New Connect ...");

	public MyButton getUsableButton(boolean isClient){
		MyButton button = newButton;
		for(;;button = button.getNextButton()){
			if (!button.isVisible()){
				break;
			}
		}
		if (isClient) button.addMouseListener(new ConnectButtonActionListener(logArea, button));
		return button;
	}

	public MyButton openNewConnectButton(String text, boolean isClient){
		MyButton button = getUsableButton(isClient);
		button.setText(text);
		button.setVisible(true);
		return button;
	}
	
	public JPanel createHeaderPanel() {
		JPanel myMainPanel = new JPanel();

		JPanel servicePanel = new JPanel();
		JButton startButton = new JButton("启动服务");
		startButton.setPreferredSize(new Dimension(200, 30));
		startButton.setBackground(Color.green);

		JButton stopButton = new JButton();
		stopButton.setPreferredSize(new Dimension(30, 30));
		stopButton.setBackground(Color.gray);
		stopButton.setEnabled(false);

		servicePanel.add(startButton);
		servicePanel.add(stopButton);

		GroupLayout mainLayout = new GroupLayout(myMainPanel);
		myMainPanel.setLayout(mainLayout);

		GroupLayout.SequentialGroup mainHGroup = mainLayout.createSequentialGroup();
		GroupLayout.SequentialGroup mainVGroup = mainLayout.createSequentialGroup();

		mainHGroup.addGroup(mainLayout.createParallelGroup().addComponent(servicePanel));
		mainLayout.setHorizontalGroup(mainHGroup);
		mainVGroup.addGroup(mainLayout.createParallelGroup().addComponent(servicePanel));
		mainLayout.setVerticalGroup(mainVGroup);
		mainLayout.linkSize(SwingConstants.VERTICAL, servicePanel);

		startButton.addActionListener(new StartButtonActionListener(logArea, startButton, stopButton));
		stopButton.addActionListener(new StopButtonActionListener(logArea, startButton, stopButton));
		return  myMainPanel;
	}

	public JScrollPane createCenterPanel() {
		final JScrollPane jPanel = new JScrollPane();
		jPanel.setSize(new Dimension(200, 300));
		ScrollPaneLayout layout = new ScrollPaneLayout();
		jPanel.setLayout(layout);
		JPanel panel = new JPanel();
		panel.setLayout(new GridLayout(50,1));
		panel.add(newButton);
		MyButton tmpButton = newButton;
		for (int i = 0; i < 49; i++) {
			MyButton a = new MyButton(String.valueOf(i));
			tmpButton.setNextButton(a);
			a.setVisible(false);
			a.setBackground(Color.red);
			panel.add(a);
			tmpButton = a;
		}
		jPanel.setViewportView(panel);
		newButton.addActionListener(new CreateNewConnectButtonActionListener(jPanel));
		return jPanel;
	}

	public JScrollPane createDownPanel() {
		JScrollPane jScrollPane = new JScrollPane();
		jScrollPane.setViewportView(logArea);
		return jScrollPane;
	}

	public TableModel getModel() {
		return model;
	}

	public void setModel(TableModel model) {
		this.model = model;
	}

	public JTextArea getLogArea() {
		return logArea;
	}
}